<?php
namespace app\admin\controller;
use think\Controller;
use app\admin\controller\Common;
use app\service\orderService;
use app\service\apiService;
use app\service\chargeRuleService;
use app\service\camService;
use app\service\wechatCarService;
use printing\printing;
use think\Db;
use think\Log;
use think\request;
use printing\printDemo;
ini_set('max_execution_time','300');
class License extends Controller{
	public function opencommand()
	{
		return '{ "Response_AlarmInfoPlate": 
					{
						"info": "ok",
						"serialData": ""	
					} 
				}';
	}
	public function coupon(){
	    $path=dir(getcwd())->path.'/license/'.date('Ymd');
		if (! file_exists ( $path )) {
			mkdir ( "$path", 0777, true );
		}
        $address = $path."/".date('His').rand(100000,999999).'.txt';
		file_put_contents($address,file_get_contents("php://input"),FILE_APPEND);
		$licenseinfo = array();
        $data = array();
        $system = "";
		//臻实相机和迁移的数据
		$datastr=file_get_contents($address);
		if(!mb_check_encoding($datastr, 'UTF-8')){
			$datastr=iconv('GB2312', 'UTF-8', $datastr);
		}
        $licenseinfo = $this->object_to_array(json_decode($datastr));
		//var_dump($licenseinfo);
        $car_number = $licenseinfo['AlarmInfoPlate']['result']['PlateResult']['license'];//车牌
        $serialno = $licenseinfo['AlarmInfoPlate']['serialno'];//设备号
        //查询是否是优惠券车辆
        $caminfo=Db::table("parking_camera")->where("serialno",$serialno)->find();
        if(!empty($caminfo)){
            $couponinfo=Db::table("car_coupon")->where("parkingNo",$caminfo["parkingNo"])->where("carPlateNo",$car_number)->where("coupon_status","1")->find();
            if(!empty($couponinfo)){
                return '{ "Response_AlarmInfoPlate": 
					{
						"info": "ok",
						"serialData": ""	
					} 
				}';
            }
        }
	}
	

    //产生随机字符串，不长于32位
    public function createNoncestr($length = 32){
        $chars = "abcdefghijklmnopqrstuvwxyz0123456789";
        $str = "";
        for ($i = 0; $i < $length; $i++)
        {
            $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
        }

        return $str;
    }

    //生成签名
    protected function MakeSign($arr,$wxkey){
        //签名步骤一：按字典序排序参数
        ksort($arr);
        $string = $this->ToUrlParams($arr);
        //签名步骤二：在string后加入KEY
        $string = $string . "&key=" . $wxkey;
        //签名步骤三：MD5加密
        $string = md5($string);
        //签名步骤四：所有字符转为大写
        $result = strtoupper($string);
        return $result;
    }

    //生成签名
    protected function MakeSigns($arr,$wxkey){
        //签名步骤一：按字典序排序参数
        ksort($arr);
        $string = $this->ToUrlParams($arr);
        //签名步骤二：在string后加入KEY
        $string = $string . "&key=" . $wxkey;
        //签名步骤三：MD5加密
        $result = md5($string);
        return $result;
    }

    //输出xml字符
    public function ToXml($arr){
        $xml = "<xml>";
        foreach ($arr as $key => $val){
            if (is_numeric($val)){
                $xml .= "<" . $key . ">" . $val . "</" . $key . ">";
            }else{
                $xml .= "<" . $key . "><![CDATA[" . $val . "]]></" . $key . ">";
            }
        }
        $xml .= "</xml>";
        return $xml;
    }

    //接收post数据
    public function postdata(){
        $receipt = $_REQUEST;
        if($receipt==null){
            $receipt = file_get_contents("php://input");
            if($receipt == null) {
                $receipt = $GLOBALS['HTTP_RAW_POST_DATA'];
            }
        }
        return $receipt;
    }

    //格式化参数格式化成url参数
    public function ToUrlParams($value){
        $buff = "";
        foreach($value as $k => $v){
            if($k != "sign" && $v != "" && !is_array($v)){
                $buff .= $k . "=" . $v . "&";
            }
        }
        $buff = trim($buff, "&");
        return $buff;
    }

    //将对象转换成数组
    public function object_to_array($obj){
        $_arr = is_object($obj) ? get_object_vars($obj) :$obj;
        foreach($_arr as $key=>$val){
            if($key !== 'gioouts'){
                $val = (is_array($val) || is_object($val)) ? $this->object_to_array($val):$val;
                $arr[$key] = $val;
            }
        }
        return $arr;
    }
	
	
	public function yzl_fsockopen($url,$post_data=array(),$cookie=array()){
        $url_arr = parse_url($url);
        $port = isset($url_arr['port'])?$url_arr['port']:80;
        if($url_arr['scheme'] == 'https'){
            $url_arr['host'] = 'ssl://'.$url_arr['host'];
        }
        $fp = fsockopen($url_arr['host'],$port,$errno,$errstr,30);
        if(!$fp) return false;
        $getPath = isset($url_arr['path'])?$url_arr['path']:'/index.php';
        $getPath .= isset($url_arr['query'])?'?'.$url_arr['query']:'';
        $method = 'GET';  //默认get方式
        if(!empty($post_data)) $method = 'POST';
        $header = "$method  $getPath  HTTP/1.1\r\n";
        $header .= "Host: ".$url_arr['host']."\r\n";
        if(!empty($cookie)){  //传递cookie信息
            $_cookie = strval(NULL);
            foreach($cookie AS $k=>$v){
                $_cookie .= $k."=".$v.";";
            }
            $cookie_str = "Cookie:".base64_encode($_cookie)."\r\n";
            $header .= $cookie_str;
        }
        if(!empty($post_data)){  //传递post数据
            $_post = array();
            foreach($post_data AS $_k=>$_v){
                $_post[] = $_k."=".urlencode($_v);
            }
            $_post = implode('&', $_post);
            $post_str = "Content-Type:application/x-www-form-urlencoded; charset=UTF-8\r\n";
            $post_str .= "Content-Length: ".strlen($_post)."\r\n";  //数据长度
            $post_str .= "Connection:Close\r\n\r\n";
            $post_str .= $_post;  //传递post数据
            $header .= $post_str;
        }else{
            $header .= "Connection:Close\r\n\r\n";
        }
        fwrite($fp, $header);
        //echo fread($fp,1024);
        usleep(1000); // 这一句也是关键，如果没有这延时，可能在nginx服务器上就无法执行成功
        fclose($fp);
        return true;
    }
  
	
}